<%-- 
    Document   : update-question-action
    Created on : Feb 13, 2012, 11:42:10 AM
    Author     : PhucAn
--%>
<%@page import="dao.AnswerDAO"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="dto.Answer"%>
<%@page import="dto.Question"%>
<%@page import="util.SharingUtil"%>
<%@page import="dao.QuestionDAO"%>
<%
    String deleteAction = request.getParameter("delete");
    String updateAction = request.getParameter("update");
    QuestionDAO daoQuesion = new QuestionDAO();
    int id = Integer.parseInt(request.getParameter("id"));
    int cateId = Integer.parseInt(request.getParameter("cateid"));
    if (deleteAction != null) {
        if (daoQuesion.delete(id) > 0) {
            session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Question is deleted");
            response.sendRedirect("../admin/index.jsp?pcontent=manage-question");
        } else {
            session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Error occured. Question cannot be deleted");
            response.sendRedirect("../admin/index.jsp?pcontent=modify-question&id=" + String.valueOf(id) + "&cateid=" + String.valueOf(cateId));
        }
    } else if (updateAction != null) {
        Question q = new Question();
        String[] answers = request.getParameterValues("answer");
       String singleAns = request.getParameter("single-ans-result");
        String[] multiAns = request.getParameterValues("multi-ans-result");
        String[] ansIds=request.getParameterValues("ans-id");
        String level=request.getParameter("level");
        //boolean isSingle = !singleAns.toLowerCase().equals("on");
        boolean isSingle=(request.getParameter("multi-choice")==null);
        String questionContent = request.getParameter("content");
        q.setId(id);
        q.setContent(request.getParameter("content"));
        q.getCategory().setId(Integer.parseInt(request.getParameter("category-id")));
        q.setSingleChoice(isSingle);
        for (String st : answers) {
            Answer ans = new Answer();
            ans.setContent(st);
            q.getAnswers().add(ans);
        }
        for(int i=0;i<ansIds.length;i++){
            q.getAnswers().get(i).setId(Integer.parseInt(ansIds[i]));
        }
        q.setLevel(Byte.parseByte(level));
        q.setMark(Float.parseFloat(request.getParameter("mark")));
        if (isSingle) {
            q.getAnswers().get(Integer.parseInt(singleAns) - 1).setIsTrue(true);
        } else {
            for (String st : multiAns) {
                int idx = Integer.parseInt(st);
                q.getAnswers().get(idx - 1).setIsTrue(true);
            }
        }
        if (daoQuesion.update(q) > 0) {
            AnswerDAO daoAnswer=new AnswerDAO();
            daoAnswer.update(q);
            SharingUtil.showMessage(request, "Your change is saved");
        } else {
            SharingUtil.showMessage(request, "Error occured. Action failed");
        }
        response.sendRedirect("../admin/index.jsp?pcontent=modify-question&id=" + String.valueOf(id) + "&cateid=" + String.valueOf(cateId));
    }
%>
